package boostrap

import (
	"flag"
	"fmt"
	"github.com/wonderivan/logger"
	"gopkg.in/yaml.v2"
	"io/ioutil"
	"ucenter/app"
	"ucenter/app/service"
)

var (
	confPath  string
	Debug     bool
	Version   string
	BuildTime string
)

func init() {
	flag.StringVar(&confPath, "conf", "conf/config.yml", "config file path, default: conf/config.yaml")
	flag.BoolVar(&Debug, "debug", false, "server debug or not, value: true/false")
	flag.Parse()
	InitConf()
	logger.SetLogger(app.Conf.LogConf)
	InitService()
}

func InitConf() {
	var (
		err error
		bs  []byte
	)
	bs, err = ioutil.ReadFile(confPath)
	if err != nil {
		panic(fmt.Sprintf("config file load fail: %s", err.Error()))
	}
	app.Conf = &app.AppConf{}
	err = yaml.Unmarshal(bs, app.Conf)
	if err != nil {
		panic(fmt.Sprintf("config unmarshal fail: %s", err.Error()))
	}
	logger.Debug("conf inited")
}

func InitService() {
	service.NewService(app.Conf)
}
